//
//  PDFDocumentPreviewGridViewCell.m
//  Cachalot
//
//  Created by Adam Cue on 6/30/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PDFDocumentPreviewGridViewCell.h"

#define TITLE_LABEL_FRAME         CGRectMake(0, 202, 180, 56)

@interface PDFDocumentPreviewGridViewCell (Private)
#pragma mark SetupMethods
- (void) setupTitleLabel;
- (void) setupPreviewView;
@end

@implementation PDFDocumentPreviewGridViewCell

@synthesize titleLabel = _titleLabel;
@synthesize asyncDocumentPreviewView = _asyncDocumentPreviewView;
@synthesize superViewController;

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
   self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier];
   if(self){
     self.backgroundColor = [UIColor clearColor];
     self.selectionStyle = AQGridViewCellSelectionStyleNone;
     self.contentView.backgroundColor = [UIColor clearColor];
     [self setupTitleLabel];
     [self setupPreviewView];
   }
   return self;
}

- (void)dealloc{
   self.titleLabel = nil;
   self.asyncDocumentPreviewView = nil;
   self.superViewController = nil;
  
   [super dealloc];
}

- (void)prepareForReuse{
   [super prepareForReuse];
   
   //[self.asyncDocumentPreviewView setDisplayImage:nil];
}

#pragma mark SetupMethods
- (void)setupTitleLabel{
   _titleLabel = [[UILabel alloc] initWithFrame:TITLE_LABEL_FRAME];
   self.titleLabel.textAlignment = UITextAlignmentCenter;
   self.titleLabel.font = [UIFont systemFontOfSize:12];
   self.titleLabel.textColor = [UIColor whiteColor];
   self.titleLabel.backgroundColor = [UIColor clearColor];
   [self.contentView addSubview:self.titleLabel];
}

- (void)setupPreviewView{
   _asyncDocumentPreviewView = [[PDFDocumentPreviewView alloc] initWithFrame:CGRectMake(24, 0, 144, 317) index:0];
   [self.contentView addSubview:self.asyncDocumentPreviewView];
}

#pragma mark ExternalMethods
+ (void)fillCell:(PDFDocumentPreviewGridViewCell *)cell forSavedReading:(Reading *)reading atIndex:(NSInteger)index{
   [cell.asyncDocumentPreviewView setDelegate:cell.superViewController];
   cell.asyncDocumentPreviewView.index = index;
   [cell.asyncDocumentPreviewView setDisplayImage:[UIImage imageWithData:[reading thumbnailImage]]];
   [cell.asyncDocumentPreviewView setSaved:YES];
   cell.titleLabel.text = reading.name;
}

+ (void)fillCell:(PDFDocumentPreviewGridViewCell *)cell forReading:(NSDictionary *)reading atIndex:(NSInteger)index{
  [cell.asyncDocumentPreviewView setDelegate:cell.superViewController];
  cell.asyncDocumentPreviewView.index = index;
  [cell.asyncDocumentPreviewView loadImageFromURL:[NSURL URLWithString:[reading objectForKey:@"thumbnail_path"]] withDefaultImage:nil];
  cell.titleLabel.text = [reading objectForKey:@"name"];
}

@end
